#include "dominion.h"
#include "dominion_helpers.h"
#include "sput/sput.h"
#include <malloc.h>
#include <stdlib.h>
#include <time.h>

struct gameState *testState;

static void test_buy_card(void)
{
    int numPlayers;
    int supplyPos;
    int card_cost;
    int res;

    int kc[10] = {adventurer, council_room, feast, gardens, mine, remodel,
                  smithy, village, baron, great_hall
                 };

    for(numPlayers = 2; numPlayers <= MAX_PLAYERS; numPlayers++) {
        initializeGame(numPlayers, kc, rand(), testState);
        // iterate though all cards
        for(supplyPos = curse; supplyPos <= treasure_map; supplyPos++) {
            // get card cost (hopefully getCost() is reliable!)
            card_cost = getCost(supplyPos);

            // test that we can't buy cards when we have no buys left
            testState->numBuys = 0;
            sput_fail_unless(buyCard(supplyPos, testState) == -1, "fail to buy card when there are 0 buys left");

            // test that we can't buy cards when there aren't any
            testState->supplyCount[supplyPos] = 0;
            sput_fail_unless(buyCard(supplyPos, testState) == -1, "fail to buy card when there are 0 such cards");

            // test that we can't buy cards when we don't have enough money
            testState->coins = 0;
            sput_fail_unless(buyCard(supplyPos, testState) == -1, "fail to buy card when there are 0 coins");

            // test that we CAN buy a card when we have a buy left, there are
            // cards left, and we have enough money
            testState->numBuys = 1;
            testState->supplyCount[supplyPos] = 1;
            testState->coins = card_cost;

            res = buyCard(supplyPos, testState);
            sput_fail_unless(res == 0, "buy card when we have enough money, enough buys, and enough cards remaining");
            sput_fail_unless(testState->phase == 1, "set phase to 1");
            sput_fail_unless(testState->numBuys == 0, "properly decrement buys");
            sput_fail_unless(testState->supplyCount[supplyPos] == 0, "properly decrement card count");
            sput_fail_unless(testState->coins == 0, "properly decrement coin count");
        }
    }

}

int main(int argc, char *argv[])
{
    // Seed random number generator
    srand(time(NULL));

    // Initialize game state
    testState = newGame();

    sput_start_testing();
    sput_enter_suite(__FILE__);

    sput_run_test(test_buy_card);
    sput_finish_testing();

    free(testState);

    return sput_get_return_value();
}
